home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / cons.handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  4.7 KB  |  178 lines

  1. /* Client interface for General purpose Linux console save/restore server
  2.    Copyright (C) 1994 Janne Kukonlehto <jtklehto@stekt.oulu.fi>
  3.    
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* The cons saver can't have a pid of 1, used to prevent bunches of */
  19. /*#ifdef linux */
  20. #include <config.h>
  21.  
  22. int    cons_saver_pid = 1;
  23.  
  24. #if defined(linux) || defined(__linux__)
  25.  
  26. #include "tty.h"
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <signal.h>
  32. #include "util.h"
  33. #include "win.h"
  34. #include "cons.saver.h"
  35.  
  36. signed char console_flag = 0;
  37.  
  38. static int pipefd1 [2] = {-1, -1}, pipefd2 [2] = {-1, -1};
  39.  
  40. void show_console_contents (int starty, int begin_line, int end_line)
  41. {
  42.     unsigned int message = 0;
  43.     int bytes = 0;
  44.     int i;
  45.  
  46.     standend ();
  47.     /* Is tty console? */
  48.     if (!console_flag)
  49.     return;
  50.     /* Paranoid: Is the cons.saver still running? */
  51.     if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT)){
  52.     cons_saver_pid = 0;
  53.     console_flag = 0;
  54.     return;
  55.     }
  56.  
  57.     /* Send command to the console handler */
  58.     message = CONSOLE_CONTENTS;
  59.     write (pipefd1[1], &message, 1);
  60.     /* Check for outdated cons.saver */
  61.     read (pipefd2[0], &message, 1);
  62.     if (message != CONSOLE_CONTENTS)
  63.     return;
  64.  
  65.     /* Send the range of lines that we want */
  66.     write (pipefd1[1], &begin_line, 1);
  67.     write (pipefd1[1], &end_line, 1);
  68.     /* Read the corresponding number of bytes */
  69.     read (pipefd2[0], &bytes, 2);
  70.  
  71.     /* Read the bytes and output them */
  72.     for (i = 0; i < bytes; i++){
  73.     if ((i % COLS) == 0)
  74.         move (starty+(i/COLS), 0);
  75.     read (pipefd2[0], &message, 1);
  76.     addch (message);
  77.     }
  78.  
  79.     /* Read the value of the console_flag */
  80.     read (pipefd2[0], &message, 1);
  81. }
  82.  
  83. void handle_console (int action)
  84. {
  85.     char *tty_name;
  86.  
  87.     switch (action){
  88.     case CONSOLE_INIT:
  89.     /* Close old pipe ends in case it is the 2nd time we run cons.saver */
  90.     close (pipefd1[1]);
  91.     close (pipefd2[0]);
  92.     /* Create two pipes for communication */
  93.     pipe (pipefd1);
  94.     pipe (pipefd2);
  95.     /* Get the console saver running */
  96.     cons_saver_pid = fork ();
  97.     if (cons_saver_pid < 0){
  98.         /* Can't fork */
  99.         /* Delete pipes */
  100.         close (pipefd1[1]);
  101.         close (pipefd1[0]);
  102.         close (pipefd2[1]);
  103.         close (pipefd2[0]);
  104.         console_flag = 0;
  105.     } else if (cons_saver_pid > 0){
  106.         /* Parent */
  107.         /* Close the extra pipe ends */
  108.         close (pipefd1[0]);
  109.         close (pipefd2[1]);
  110.         /* Was the child successful? */
  111.         read (pipefd2[0], &console_flag, 1);
  112.         if (!console_flag){
  113.         close (pipefd1[1]);
  114.         close (pipefd2[0]);
  115.         waitpid (cons_saver_pid, &action, 0);
  116.         }
  117.     } else {
  118.         /* Child */
  119.         /* Close the extra pipe ends */
  120.         close (pipefd1[1]);
  121.         close (pipefd2[0]);
  122.         tty_name = ttyname (0);
  123.         /* Bind the pipe 0 to the standard input */
  124.         close (0);
  125.         dup (pipefd1[0]);
  126.         close (pipefd1[0]);
  127.         /* Bind the pipe 1 to the standard output */
  128.         close (1);
  129.         dup (pipefd2[1]);
  130.         close (pipefd2[1]);
  131.         /* Bind standard error to /dev/null */
  132.         close (2);
  133.         open ("/dev/null", O_WRONLY);
  134.         /* Exec the console save/restore handler */
  135.         execl (LIBDIR "bin/cons.saver", "cons.saver", tty_name, NULL);
  136.         /* Exec failed */
  137.         console_flag = 0;
  138.         write (1, &console_flag, 1);
  139.         close (1);
  140.         close (0);
  141.         exit (3);
  142.     } /* if (cons_saver_pid ...) */
  143.     break;
  144.  
  145.     case CONSOLE_DONE:
  146.     case CONSOLE_SAVE:
  147.     case CONSOLE_RESTORE:
  148.     /* Is tty console? */
  149.     if (!console_flag)
  150.         return;
  151.     /* Paranoid: Is the cons.saver still running? */
  152.     if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT)){
  153.         cons_saver_pid = 0;
  154.         console_flag = 0;
  155.         return;
  156.     }
  157.     /* Send command to the console handler */
  158.     write (pipefd1[1], &action, 1);
  159.     if (action != CONSOLE_DONE){
  160.         /* Wait the console handler to do its job */
  161.         read (pipefd2[0], &console_flag, 1);
  162.     }
  163.     if (action == CONSOLE_DONE || !console_flag){
  164.         /* We are done -> Let's clean up */
  165.         close (pipefd1 [1]);
  166.         close (pipefd2 [0]);
  167.         waitpid (cons_saver_pid, &action, 0);
  168.         console_flag = 0;
  169.     }
  170.     break;
  171.     default:
  172.     /* Nothing */
  173.     }
  174. }
  175.  
  176. #endif /* #ifdef linux */
  177.  
  178.